/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Room;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

/**
 *
 * @author daz
 */
public class RoomDao extends SimpleJdbcDaoSupport {
    
    public List<Room> getListRoom() {
        String query = "SELECT * FROM rooms";
        return getSimpleJdbcTemplate().query(query, new RoomMapper());
    }
    
    public void insertNewRoom(Room r) {
        String query = "INSERT INTO rooms(name) VALUES('" + r.getName() + "')";
        getJdbcTemplate().execute(query);
    }
    
    private class RoomMapper implements ParameterizedRowMapper<Room> {
        
        @Override
        public Room mapRow(ResultSet rs, int rowNum) throws SQLException {
            Room room = new Room();
            room.setId(rs.getInt("id"));
            room.setName(rs.getString("name"));
            return room;
        }
    }
}
